/**
 * 
 */
package cm.dpassyann.speedDating.ui.activity.start.task;

import roboguice.inject.ContextSingleton;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;
import cm.dpassyann.speedDating.ui.activity.start.IStartActivity;

import com.google.inject.Inject;

/**
 * @author deungoueyann
 *
 */
@ContextSingleton
public class StartActivityTask extends AsyncTask<Integer, Integer, Void>{

	private final IStartActivity	_activity;
	private boolean _executeOnly;
	
	@Inject
	public StartActivityTask (IStartActivity activity){
		this._activity = activity;
		this._executeOnly = true;
	}
	
	@Override
	protected void onPreExecute() {
		// initialize the progress bar
		// set maximum progress to 100.
		_activity.initProgress(100);

	}

	@Override
	protected void onCancelled() {
		// stop the progress
		_activity.resetProgress();
	}

	@Override
	protected Void doInBackground(Integer... params) {
		// get the initial starting value
		int start=params[0];
		// increment the progress
		
		for(int i=start;i<=100;i+=5){
			try {
				boolean cancelled=isCancelled();
				//if async task is not cancelled, update the progress
				if(!cancelled){
					publishProgress(i);
					SystemClock.sleep(500);
				}

			} catch (Exception e) {
				Log.e("Error", e.toString());
			}

		}
		return null;
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		// increment progress bar by progress value
		_activity.updateProgress(values[0]);
		if(values[0] == 10){
			
		}
		else if(values[0] == 30){
			
		}
		
		else if(values[0] > 50 && _executeOnly){
			_activity.showNextCopyrightText();
			_executeOnly = false;
		}
		
		else if(values[0] == 70){
			_activity.showNextLoadingText();
		}

	}

	@Override
	protected void onPostExecute(Void result) {
		// async task finished
		Log.v("Progress", "Finished");
		this.cancel(true);
		_activity.finishTask();
	}
}
